/* Open a file somewhere on PATH */

/*
    Contact info:
    bhepple@freeshell.org
    http://bhepple.freeshell.org

    Copyright (C) 1999-2008 Bob Hepple

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; see the file COPYING.  If not, write to
    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.

*/

/* Compile test version with 

cc -g -o pathopen -DTEST -DSM_SUNOS pathopen.c

or

tcc -v -y -I/tc/include -ml -DTEST -DSM_TURBOC -c pathopen.c
tcc -v -y -L/tc/lib -ml -opathopen.exe pathopen.obj mylibl.lib

*/

#include <stdio.h>
#include <string.h>

extern char *getenv();

#ifdef SM_TURBOC
#define MAXPATHLEN 128
#define MAXNAMELEN 12
#endif

#ifdef SM_XENIX
#define MAXPATHLEN 255
#define MAXNAMELEN 14
#endif

#ifdef SM_SUNOS
#define MAXPATHLEN 1023
#define MAXNAMELEN 255
#endif

#ifdef SM_SYSV
#define MAXPATHLEN 1023
#define MAXNAMELEN 255
#endif

FILE *pathopen(filename, type, fullname)
    char	*filename; /* as fopen */
    char	*type;	   /* as fopen */
    char	*fullname; /* Full name of file is copied here if not NULL */
{
    char	path[255], trial_name[MAXPATHLEN + 1];
    char	*path_component;
    char	path_sep;
    FILE	*file = NULL;

#ifdef SM_TURBOC
    path_sep = ';';
    fixname(filename);
#else
    path_sep = ':';
#endif

    path_component = getenv("PATH");
    if (path_component)
	strcpy(path, path_component);
    else
	*path = 0;

    path_component = path;
    
    while (path_component && *path_component && !file) {
	char	*next_path;

	next_path = strchr(path_component, path_sep);

	if (next_path != NULL)
	    *next_path = 0;

	strcpy(trial_name, path_component);
	if (*trial_name && trial_name[strlen(trial_name) - 1] != '/')
	    strcat(trial_name, "/");
	strcat(trial_name, filename);
#ifdef SM_TURBOC
	if (strchr(filename, '.') == NULL)
	    strcat(trial_name, ".com");
#endif
	file = fopen(trial_name, type);
#ifdef SM_TURBOC
	if (file == NULL) {
	    strcpy(strrchr(trial_name, '.'), ".exe");
	    file = fopen(trial_name, type);
	}
#endif
	path_component = next_path? next_path + 1: NULL;
    }

    if (fullname) {
	*fullname = 0;
	if (file)
	    strcpy(fullname, trial_name);
    }

    return(file);
}

#ifdef TEST
main(argc, argv)
{
    char	filename[MAXPATHLEN + 1];
    FILE	*f;

    while (1) {
	printf("Enter filename\n");
	gets(filename);
	f = pathopen(filename, "r", filename);
	if (f) {
	    fclose(f);
	    printf("OK: filename = %s\n", filename);
	} else
	    printf("Failed\n");
    }
}
#endif /* TEST */

/* For emacs:
 * Local Variables:
 * tab-width: 8
 * End:
 */
